{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## resize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\zihao\\AppData\\Local\\Temp\\ipykernel_32548\\2566087526.py:24: DeprecationWarning: ANTIALIAS is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.LANCZOS instead.\n",
      "  img_resized = img.resize((640, 640), Image.ANTIALIAS)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Resizing done.\n"
     ]
    }
   ],
   "source": [
    "from PIL import Image\n",
    "import os\n",
    "\n",
    "# The folder containing the images to resize\n",
    "input_folder = 'soil'\n",
    "# The folder to save the resized images\n",
    "output_folder = 'soil_resized'\n",
    "\n",
    "# Create output folder if it doesn't exist\n",
    "if not os.path.exists(output_folder):\n",
    "    os.makedirs(output_folder)\n",
    "\n",
    "# Loop through each file in the input folder\n",
    "for filename in os.listdir(input_folder):\n",
    "    if filename.endswith(('.jpg', '.png', '.jpeg')):  # You can add more formats here\n",
    "        # Create a path for the input and output files\n",
    "        input_filepath = os.path.join(input_folder, filename)\n",
    "        output_filepath = os.path.join(output_folder, filename)\n",
    "\n",
    "        # Open the image\n",
    "        img = Image.open(input_filepath)\n",
    "\n",
    "        # Resize the image\n",
    "        img_resized = img.resize((640, 640), Image.ANTIALIAS)\n",
    "\n",
    "        # Save the resized image\n",
    "        img_resized.save(output_filepath)\n",
    "\n",
    "print(\"Resizing done.\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ramdom sample data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Copied 200 random images and their corresponding labels to data_600\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import random\n",
    "import shutil\n",
    "\n",
    "# Define the source and destination folders\n",
    "src_folder = \"data_1200\"\n",
    "dst_folder = \"data_600\"\n",
    "\n",
    "# Define the sub-folders for images and labels\n",
    "img_folder = \"images\"\n",
    "label_folder = \"labels\"\n",
    "\n",
    "# Create destination folders if they do not exist\n",
    "if not os.path.exists(dst_folder):\n",
    "    os.mkdir(dst_folder)\n",
    "if not os.path.exists(os.path.join(dst_folder, img_folder)):\n",
    "    os.mkdir(os.path.join(dst_folder, img_folder))\n",
    "if not os.path.exists(os.path.join(dst_folder, label_folder)):\n",
    "    os.mkdir(os.path.join(dst_folder, label_folder))\n",
    "\n",
    "# List files in the source image and label folders\n",
    "src_img_files = [f for f in os.listdir(os.path.join(src_folder, img_folder)) if f.endswith('.jpg')]\n",
    "src_label_files = [f for f in os.listdir(os.path.join(src_folder, label_folder)) if f.endswith('.txt')]\n",
    "\n",
    "# Randomly select 200 images (and their corresponding labels)\n",
    "selected_files = random.sample(src_img_files, 600)\n",
    "\n",
    "# Copy selected files into the new folder\n",
    "for filename in selected_files:\n",
    "    # Construct the base filename without extension\n",
    "    base_filename = os.path.splitext(filename)[0]\n",
    "    \n",
    "    # Copy the image file\n",
    "    img_src_path = os.path.join(src_folder, img_folder, filename)\n",
    "    img_dst_path = os.path.join(dst_folder, img_folder, filename)\n",
    "    shutil.copy2(img_src_path, img_dst_path)\n",
    "    \n",
    "    # Copy the corresponding label file\n",
    "    label_filename = base_filename + '.txt'\n",
    "    label_src_path = os.path.join(src_folder, label_folder, label_filename)\n",
    "    label_dst_path = os.path.join(dst_folder, label_folder, label_filename)\n",
    "    shutil.copy2(label_src_path, label_dst_path)\n",
    "\n",
    "print(f\"Copied 200 random images and their corresponding labels to {dst_folder}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## add real"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Successfully merged contents of train into data_1200\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import shutil\n",
    "\n",
    "# Define the source (A) and destination (B) folders\n",
    "folder_A = \"train\"\n",
    "folder_B = \"data_1200\"\n",
    "\n",
    "# Define the sub-folders for images and labels\n",
    "img_folder = \"images\"\n",
    "label_folder = \"labels\"\n",
    "\n",
    "# List files in the source image and label folders\n",
    "src_img_files_A = [f for f in os.listdir(os.path.join(folder_A, img_folder)) if f.endswith('.jpg')]\n",
    "src_label_files_A = [f for f in os.listdir(os.path.join(folder_A, label_folder)) if f.endswith('.txt')]\n",
    "\n",
    "# Check if B's subfolders exist, if not, create them\n",
    "if not os.path.exists(os.path.join(folder_B, img_folder)):\n",
    "    os.mkdir(os.path.join(folder_B, img_folder))\n",
    "if not os.path.exists(os.path.join(folder_B, label_folder)):\n",
    "    os.mkdir(os.path.join(folder_B, label_folder))\n",
    "\n",
    "# Copy image files from A to B\n",
    "for img_file in src_img_files_A:\n",
    "    img_src_path_A = os.path.join(folder_A, img_folder, img_file)\n",
    "    img_dst_path_B = os.path.join(folder_B, img_folder, img_file)\n",
    "    shutil.copy2(img_src_path_A, img_dst_path_B)\n",
    "\n",
    "# Copy label files from A to B\n",
    "for label_file in src_label_files_A:\n",
    "    label_src_path_A = os.path.join(folder_A, label_folder, label_file)\n",
    "    label_dst_path_B = os.path.join(folder_B, label_folder, label_file)\n",
    "    shutil.copy2(label_src_path_A, label_dst_path_B)\n",
    "\n",
    "print(f\"Successfully merged contents of {folder_A} into {folder_B}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import shutil\n",
    "\n",
    "# Define the paths to the image and label folders\n",
    "images_folder = 'test/images'\n",
    "labels_folder = 'test/all_label'\n",
    "selected_labels_folder = 'test/labels'\n",
    "\n",
    "# Create the selected_labels folder if it doesn't exist\n",
    "os.makedirs(selected_labels_folder, exist_ok=True)\n",
    "\n",
    "# List the image files in the images folder\n",
    "image_files = [f for f in os.listdir(images_folder) if f.endswith('.jpg')]\n",
    "\n",
    "# Iterate through the image files and move corresponding label files\n",
    "for image_file in image_files:\n",
    "    image_name = os.path.splitext(image_file)[0]  # Get the image file name without extension\n",
    "    label_file = image_name + '.txt'  # Corresponding label file name\n",
    "    \n",
    "    # Check if the label file exists in the labels folder\n",
    "    if os.path.exists(os.path.join(labels_folder, label_file)):\n",
    "        # Move the label file to the selected_labels folder\n",
    "        shutil.move(os.path.join(labels_folder, label_file), os.path.join(selected_labels_folder, label_file))\n",
    "        print(f\"Moved {label_file} to {selected_labels_folder}\")\n",
    "    else:\n",
    "        print(f\"No matching label file found for {image_file}\")\n",
    "\n",
    "print(\"Label selection completed.\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "python39",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "0.0.0"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
